package cn.jiyun.service.impl;

import cn.jiyun.entity.GoodsCombo;
import cn.jiyun.entity.GoodsList;
import cn.jiyun.mapper.GoodsComboMapper;
import cn.jiyun.mapper.GoodsListMapper;
import cn.jiyun.service.IGoodsListService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author author
 * @since 2024-11-21
 */
@Service
public class GoodsListServiceImpl extends ServiceImpl<GoodsListMapper, GoodsList> implements IGoodsListService {

    @Autowired
    private GoodsListMapper goodsListMapper;
    @Autowired
    private GoodsComboMapper goodsComboMapper;
    @Override
    public Page<GoodsList> findpage(Integer current, Integer pageSize, GoodsList goodsList) {
        Page<GoodsList> goodsListPage = new Page<>(current,pageSize);
        LambdaQueryWrapper<GoodsList> qw = new LambdaQueryWrapper<>();
//        商品id
        if (goodsList.getId()!=null){
            qw.eq(GoodsList::getId,goodsList.getId());
        }
//        商品名称
        if (goodsList.getItem()!=null&&goodsList.getItem().length()>0){
            qw.like(GoodsList::getItem,goodsList.getItem()  );
        }
//        店铺名称
        if (goodsList.getShop()!=null&&goodsList.getShop().length()>0){
            qw.like(GoodsList::getShop,goodsList.getShop());
        }
//        审核状态
        if (goodsList.getReviewstatus()!=null){
            qw.eq(GoodsList::getReviewstatus,goodsList.getReviewstatus());
        }
//        审核人
        if (goodsList.getReviewer()!=null&&goodsList.getReviewer().length()>0){
            qw.like(GoodsList::getReviewer,goodsList.getReviewer());
        }
//        价格
        if (goodsList.getMinprice()!=null){
            qw.gt(GoodsList::getPrice,goodsList.getMinprice());
        }
        if (goodsList.getMaxprice()!=null){
            qw.lt(GoodsList::getPrice,goodsList.getMaxprice());
        }
//        销量
        if (goodsList.getMinMonthlySales()!=null){
            qw.gt(GoodsList::getMonthlysales,goodsList.getMinMonthlySales());
        }
        if (goodsList.getMaxMonthlySales()!=null){
            qw.lt(GoodsList::getMonthlysales,goodsList.getMaxMonthlySales());
        }
//        类型
        if (goodsList.getGoodtype()!=null&&goodsList.getGoodtype().length()>0){
            qw.eq(GoodsList::getGoodtype,goodsList.getGoodtype());
        }
//
        if(goodsList.getClassification()!=null){
            qw.eq(GoodsList::getClassification,goodsList.getClassification());
        }
//        商品出售状态
        if(goodsList.getGoodstatus()!=null){
            qw.eq(GoodsList::getGoodstatus,goodsList.getGoodstatus());
        }
        if(goodsList.getGoodstatus2()==true){
            qw.ne(GoodsList::getGoodstatus,4);
        }
//        日期
        if (goodsList.getZtime() != null) {
            LocalDateTime now = LocalDateTime.now();
            int mo = 90;
            if (goodsList.getZtime()==1) {
                    qw.apply("ABS(DATEDIFF({0}, createtime)) <= {1}", now, mo);
            }
            if(goodsList.getZtime()==2){
                qw.apply("YEAR(createtime)=YEAR(CURRENT_DATE)");
            }
            if(goodsList.getZtime()==3){
                qw.apply("DATE_FORMAT(createtime,'%Y')=2022");
            }
            if(goodsList.getZtime()==4){
                qw.apply("DATE_FORMAT(createtime,'%Y')=2021");
            }
            if(goodsList.getZtime()==5){
                qw.apply("DATE_FORMAT(createtime,'%Y')=2020");
            }
            if(goodsList.getZtime()==6){
                qw.apply("DATE_FORMAT(createtime,'%Y')<2020");
            }
        }
        return goodsListMapper.selectPage(goodsListPage,qw);
    }

    @Override
    public List<GoodsCombo> findbyidcombo(Integer id) {
        LambdaQueryWrapper<GoodsCombo> qw = new LambdaQueryWrapper<>();
        qw.eq(GoodsCombo::getGid,id);
        return goodsComboMapper.selectList(qw);
    }
}
